package com.xia.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * 发送邮件
 * 
 * @author Administrator
 * 
 */
public class MailSender {
	Properties props = null;
	MailAuthenticator authenticator = null;

	/**
	 * 发送文本邮件
	 * 
	 * @throws MessagingException
	 */
	public void sendTextMail(MailInfo mailInfo) throws MessagingException {

		Message mailMessage = this.sendMail(mailInfo);

		// 设置发送邮件的内容
		mailMessage.setText(mailInfo.getContent());
		// 发送邮件
		// Transport.send(mailMessage);
		String one = SendMailOverTimeCheck.sendMails(mailMessage);
		System.out.println("发送状态：" + one);
		if (one.equals("overtime")) {
			System.out.println("第二次尝试发送 ");
			String two = SendMailOverTimeCheck.sendMails(mailMessage);
			if (two.equals("overtime")) {
				System.out.println("第三次尝试发送 ");
				String three = SendMailOverTimeCheck.sendMails(mailMessage);
			}
		}
	}

	/**
	 * 发送html文件
	 * 
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public void sendHtmlMail(MailInfo mailInfo) throws MessagingException,
			UnsupportedEncodingException {
		Message mailMessage = this.sendMail(mailInfo);

		// Multipart类是一个容器类，包含MimeBodyPart类型的对象
		Multipart multiPart = new MimeMultipart();

		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart bodyPart = new MimeBodyPart();

		// 创建一个包含HTML内容的MimeBodyPart
		bodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
		multiPart.addBodyPart(bodyPart);

		// 附件数组
		String[] attachs = mailInfo.getAttachFileNames();

		if (attachs.length <= 0)
			return;

		for (int i = 0; i < attachs.length; i++) {
			if (!attachs[i].equals("")) {

				bodyPart = new MimeBodyPart();
				// 获得附件
				DataSource dataSource = new FileDataSource(attachs[i]);

				// 设置附件的数据处理器
				bodyPart.setDataHandler(new DataHandler(dataSource));

				// 设置附件名字
				bodyPart.setFileName(MimeUtility.encodeText(attachs[i]));
				multiPart.addBodyPart(bodyPart);
			}
		}
		// 将MimeMultipart对象设置为邮件内容
		mailMessage.setContent(multiPart);

		// 发送邮件
		// Transport.send(mailMessage);
		String one = SendMailOverTimeCheck.sendMails(mailMessage);
		System.out.println("发送状态：" + one);
		if (one.equals("overtime")) {
			System.out.println("第二次尝试发送 ");
			String two = SendMailOverTimeCheck.sendMails(mailMessage);
			if (two.equals("overtime")) {
				System.out.println("第三次尝试发送 ");
				String three = SendMailOverTimeCheck.sendMails(mailMessage);
			}
		}
	}

	/**
	 * 公共
	 */
	public Message sendMail(MailInfo mailInfo) throws MessagingException {
		props = mailInfo.getProperties();
		// 判断是否需要身份认证
		if (mailInfo.isValidate()) {
			// 如果需要身份认证，则创建一个密码验证器
			authenticator = new MailAuthenticator(mailInfo.getUserName(),
					mailInfo.getPassword());
		}

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(props,
				authenticator);

		// 根据session创建一个邮件信息
		Message mailMessage = new MimeMessage(sendMailSession);

		// 创建邮件发送者地址
		Address fromAddress = new InternetAddress(mailInfo.getFromAddress());
		mailMessage.setFrom(fromAddress);

		// 创建邮件的接收者地址，并设置到邮件消息中，只可发送给一人
		// Address toAddress = new InternetAddress(mailInfo.getToAddress());

		// 收件人地址列表
		String[] toAddress = mailInfo.getToAddress();

		// 创建一个InternetAddress数组，长度为toAddress.length
		InternetAddress[] sendTo = new InternetAddress[toAddress.length];

		for (int i = 0; i < toAddress.length; i++) {
			System.out.println("发送到" + toAddress[i]);
			sendTo[i] = new InternetAddress(toAddress[i]);
		}

		// 设置收件人地址，Message.RecipientType.TO属性表示接收者的类型为TO
		mailMessage.setRecipients(Message.RecipientType.TO, sendTo);

		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());

		// 设置邮件发送时间
		mailMessage.setSentDate(new Date());

		return mailMessage;
	}

	public Message sendMail2(MailInfo mailInfo) throws MessagingException {
		props = mailInfo.getProperties();
		// 判断是否需要身份认证
		if (mailInfo.isValidate()) {
			// 如果需要身份认证，则创建一个密码验证器
			authenticator = new MailAuthenticator(mailInfo.getUserName(),
					mailInfo.getPassword());
		}

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(props,
				authenticator);

		// 根据session创建一个邮件信息
		Message mailMessage = new MimeMessage(sendMailSession);

		// 创建邮件发送者地址
		Address fromAddress = new InternetAddress(mailInfo.getFromAddress());
		mailMessage.setFrom(fromAddress);

		// 创建邮件的接收者地址，并设置到邮件消息中，只可发送给一人
		// Address toAddress = new InternetAddress(mailInfo.getToAddress());

		// 收件人地址列表
		String[] toAddress = mailInfo.getToAddress();

		// 创建一个InternetAddress数组，长度为toAddress.length
		InternetAddress[] sendTo = new InternetAddress[toAddress.length];

		for (int i = 0; i < toAddress.length; i++) {
			System.out.println("发送到" + toAddress[i]);
			sendTo[i] = new InternetAddress(toAddress[i]);
		}

		// 设置收件人地址，Message.RecipientType.TO属性表示接收者的类型为TO
		mailMessage.setRecipients(Message.RecipientType.TO, sendTo);

		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());

		// 设置邮件发送时间
		mailMessage.setSentDate(new Date());

		return mailMessage;
	}
}
